package mis.ds.ps.ui.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.Map;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import mis.ds.ps.controller.Result;
import mis.ds.ps.model.iface.Entities;

public class Step2Module2 extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JTabbedPane tabbedPane;
	private ISwingAdapter swingAdapter;
	private Result result;

	public Step2Module2(ISwingAdapter sa) {
		swingAdapter = sa;
		initialize();
	}

	private void initialize() {

	}

	public void actionPerformed(ActionEvent e) {

	}

	public void setResult() {

		if (tabbedPane != null) {
			remove(tabbedPane);
		}

		tabbedPane = new JTabbedPane();
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

//		if (result.getEntity().equalsIgnoreCase(Entities.DOCENTES.toString())) {
//
//			// extract avps
//			Map<String, LinkedList<Object>> map = result.getValueMap();
//			Object[] nomes = map.get("nome").toArray();
//
//			tabbedPane.addTab("Docentes", new DocentesTable(nomes));
//		}
//

		 tabbedPane.addTab("Aulas no CET", new SimpleTableDemo());
		 tabbedPane.addTab("Aulas noutro depart.", new SimpleTableDemo());
		 tabbedPane.addTab("Aulas noutra instit.", new SimpleTableDemo());
		 tabbedPane.addTab("Tempos a evitar", new SimpleTableDemo());
		 tabbedPane.addTab("Indisponibilidade", new SimpleTableDemo());

		add(tabbedPane, BorderLayout.CENTER);
	}

	public Result getResult() {
		return result;
	}

}

//class DocentesTable extends JPanel implements ActionListener {
//
//	private static final long serialVersionUID = 9057101379054858536L;
//	private JTable table; 
//	private JScrollPane scrollPane;
//	private JPopupMenu popupMenu;
//	private JMenuItem menuItemInsert;
//	private JMenuItem menuItemDelete;
//	private MouseListener popupListener;
//
//	private static String INSERT_CMD = "Insert Rows";
//	private static String DELETE_CMD = "Delete Rows";
//
//	public DocentesTable(Object[] nomes) {
//
//		super(new GridLayout(1, 0));
//
//		String[] columnNames = { "Versão", "Código", "Sigla", "Nome" };
//
//		Object[][] data = new String[nomes.length][4];
//		for (int j = 0; j < data.length; j++) {
//			data[j][3] = nomes[j].toString();
//		}
//
//		table = new JTable();
//		table.setModel(new DefaultTableModel(
//        		data, columnNames));
//
//		table.setPreferredScrollableViewportSize(new Dimension(750, 470));
//		table.setFillsViewportHeight(true);
//
//		// Create the scroll pane and add the table to it.
//		scrollPane = new JScrollPane(table);
//
//		// Add the scroll pane to this panel.
//		add(scrollPane);
//		addPopupMenu(table);
//	}
//	
//	public void addPopupMenu(JTable table) {
//		popupMenu = new JPopupMenu();
//		menuItemInsert = new JMenuItem(INSERT_CMD);
//		menuItemInsert.addActionListener(this);
//	    popupMenu.add(menuItemInsert);
//	    menuItemDelete = new JMenuItem(DELETE_CMD);
//	    menuItemDelete.addActionListener(this);
//	    popupMenu.add(menuItemDelete);
//	    
//	    popupListener = new PopupListener();
//	    table.addMouseListener(popupListener);
//	}
//	
//	class PopupListener extends MouseAdapter {
//		public void mousePressed(MouseEvent e) {
//			showPopup(e);
//		}
//		public void mouseReleased(MouseEvent e) {
//			showPopup(e);
//		}
//		private void showPopup(MouseEvent e) {
//			if (e.isPopupTrigger()) {
//				popupMenu.show(e.getComponent(), e.getX(), e.getY());
//			}
//		}
//	}
//	
//	public void actionPerformed(ActionEvent e) {
//		System.out.println(e.getActionCommand());
//		DefaultTableModel model = (DefaultTableModel) this.table.getModel();
//		if (e.getActionCommand() == INSERT_CMD) {
//			int row = table.getSelectedRow();
//			model.insertRow(row+1,new Object[]{"","","",""});
//		} else if (e.getActionCommand() == DELETE_CMD) {
//			int[] rows = table.getSelectedRows();
//			for(int i=0;i<rows.length;i++){
//				model.removeRow(rows[i]-i);
//			}
//		} 
//	}
//
//}
//
//class DisciplinasTable extends JPanel {
//
//	private static final long serialVersionUID = 9057101379054858536L;
//
//	public DisciplinasTable() {
//
//		super(new GridLayout(1, 0));
//
//		String[] columnNames = { "Versão", "Código", "Sigle", "Nome" };
//
//		Object[][] data = { { "Kathy", "Smith", "Snowboarding", "" }, { "John", "Doe", "Rowing", "" }, { "Sue", "Black", "Knitting", "" },
//				{ "Jane", "White", "Speed reading", "" }, { "Joe", "Brown", "Pool", "" } };
//
//		final JTable table = new JTable(data, columnNames);
//
//		table.setPreferredScrollableViewportSize(new Dimension(750, 470));
//		table.setFillsViewportHeight(true);
//
//		// Create the scroll pane and add the table to it.
//		JScrollPane scrollPane = new JScrollPane(table);
//
//		// Add the scroll pane to this panel.
//		add(scrollPane);
//	}
//}

class SimpleTableDemo extends JPanel {
//  private boolean DEBUG = false;

  public SimpleTableDemo() {
      super(new GridLayout(1,0));

      String[] columnNames = {"Dia ",
                              "Hora"};

      Object[][] data = {
	    {"Segunda", "8:30"},
	    {"Segunda", "9:00"}
      };

      final JTable table = new JTable(data, columnNames);
      table.setPreferredScrollableViewportSize(new Dimension(750, 470));
      table.setFillsViewportHeight(true);

//      if (DEBUG) {
//          table.addMouseListener(new MouseAdapter() {
//              public void mouseClicked(MouseEvent e) {
//                  printDebugData(table);
//              }
//          });
//      }
      
      table.addMouseListener( new MouseAdapter()
      {
      	public void mouseClicked( MouseEvent e )
      	{
      		// Left mouse click
      		if ( SwingUtilities.isLeftMouseButton( e ) )
      		{
      			// Do something
      			System.out.println(e.getPoint());
      		}
      		// Right mouse click
      		else if ( SwingUtilities.isRightMouseButton( e ))
      		{
      			// get the coordinates of the mouse click
      			Point p = e.getPoint();
       
      			// get the row index that contains that coordinate
      			int rowNumber = table.rowAtPoint( p );
       
      			// Get the ListSelectionModel of the JTable
      			ListSelectionModel model = table.getSelectionModel();
       
      			// set the selected interval of rows. Using the "rowNumber"
      			// variable for the beginning and end selects only that one row.
      			model.setSelectionInterval( rowNumber, rowNumber );
      		}
      	}
      });

      //Create the scroll pane and add the table to it.
      JScrollPane scrollPane = new JScrollPane(table);

      //Add the scroll pane to this panel.
      add(scrollPane);
  }

  
  
  private void printDebugData(JTable table) {
      int numRows = table.getRowCount();
      int numCols = table.getColumnCount();
      javax.swing.table.TableModel model = table.getModel();

      System.out.println("Value of data: ");
      for (int i=0; i < numRows; i++) {
          System.out.print("    row " + i + ":");
          for (int j=0; j < numCols; j++) {
              System.out.print("  " + model.getValueAt(i, j));
          }
          System.out.println();
      }
      System.out.println("--------------------------");
  }

  /**
   * Create the GUI and show it.  For thread safety,
   * this method should be invoked from the
   * event-dispatching thread.
   */
//  private static void createAndShowGUI() {
//      //Create and set up the window.
//      JFrame frame = new JFrame("SimpleTableDemo");
//      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
//      //Create and set up the content pane.
//      SimpleTableDemo newContentPane = new SimpleTableDemo();
//      newContentPane.setOpaque(true); //content panes must be opaque
//      frame.setContentPane(newContentPane);
//
//      //Display the window.
//      frame.pack();
//      frame.setVisible(true);
//  }

}
